/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if(head == nullptr)return 0;
        if(head->next == nullptr)return head;
        if(k == 0)return head;
        ListNode* p = head;
        int size = 0;
        int a[510] = {0};
        while(p!=nullptr)
        {
            a[size] = p->val;
            p = p->next;
            size++;
        }
        k = k %size;
        if(k == 0)return head;
        int index = size-k;
        p = head;
         while(p!=nullptr)
        {
            p->val = a[index];
            p = p->next;
            index++;
            if(index == size)
            {
                index = 0;
            }
        }
    return head;
    }
};
